﻿using UnityEngine;
using System.Collections;

//GameHex
//A 3D representation of a hex unit
public class GameHex : MonoBehaviour {

	public TextMesh labelValue;
	public TextMesh labelSkill;
	public TextMesh labelShield;
	public TextMesh labelScore;
	public Transform iconPowerUp;
	public Transform iconShield;
	//public TextMesh label1;
	//public TextMesh label2;
	//public TextMesh label3;

	private GameUnit unit;
	private GamePowerUp powerUp;
	
	private int curShield = 0;
	private int curLevelModifier = 0;

	// Use this for initialization
	void Start () {
		//labelScore.renderer.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {
	}

	public void clear() {
		labelValue.text = "";
		labelSkill.text = "";
		labelShield.text = "";
		labelScore.text = "";
		iconPowerUp.renderer.enabled = false;
		iconShield.renderer.enabled = false;
	}
	
	public void fadeOut() {
		iTween.FadeTo( gameObject, iTween.Hash( "alpha", 0, "time", 0.5f, "onComplete", "destroy") );
	}
	
	public void destroy() {
		Destroy(gameObject);
	}
	
	public void setUnit( GameUnit unit ) {
		this.unit = unit;
		hideScore();
		iconPowerUp.renderer.enabled = false;
		iconShield.renderer.enabled = false;
		labelShield.text = "";
		labelValue.text = string.Format("{0}", unit.level + curLevelModifier); //level bonus / 
		if (unit.skill == GameSkill.None)
			labelSkill.text = "";
		else {
			labelSkill.text = string.Format("{0}:{1}", unit.skill, unit.skillLevel);
		}
	}
	
	public GameUnit getUnit() {
		return unit;
	}
	
	public void setPowerUp( GamePowerUp powerUp ) {
		this.powerUp = powerUp;
		hideScore();
		Debug.Log ("GameHex.setPowerUp : power= " + powerUp.power + ", level=" + powerUp.level);
		labelValue.text = string.Format("{0}", powerUp.level);
		labelSkill.text = string.Format("{0}", powerUp.power);
		iconPowerUp.renderer.enabled = true;
		iconShield.renderer.enabled = false;
		labelShield.text = "";
		
		renderer.material.color = Color.yellow;
		labelValue.color = Color.black;
		labelSkill.color = Color.black;
	}
	
	public GamePowerUp getPowerUp() {
		return powerUp;
	}
	
	public void SetColor(PlayerColor color ) {
		renderer.material.color = Color.white;
		if (color == PlayerColor.Red)
			//renderer.material.color = Color.red;
			iTween.ColorTo( gameObject, iTween.Hash("color", Color.red, "time", 0.5f, "includechildren", false ) );
		else
			//renderer.material.color = Color.blue;
			iTween.ColorTo( gameObject, iTween.Hash("color", Color.blue, "time", 0.5f, "includechildren", false ) );
	}
	
	public void setShield(int shield) {
		// animate score
		if (curShield > 0 && shield != curShield)
			showScore( shield - curShield );
			
		curShield = shield;
		if (shield > 0) {
			iconShield.renderer.enabled = true;
			labelShield.text = string.Format("{0}", curShield);
		}
		else {
			iconShield.renderer.enabled = false;
			labelShield.text = "";
		}
	}
	
	public void setLevelModifier(int level) {
		// animate score
		if (level != curLevelModifier) {
			int diff = (level - curLevelModifier);
			showScore(diff);
		}
		
		curLevelModifier = level;
		labelValue.text = string.Format("{0}", unit.level + level);
	}
	
	public void showScore( int value ) {
		//Debug.Log( "GameHex.showScore value=" + value );
		if (value > 0)
			labelScore.text = string.Format("+{0}", value);
		else
			labelScore.text = string.Format("{0}", value);
			
		labelScore.renderer.enabled = true;
		
		iTween.MoveFrom( labelScore.gameObject, iTween.Hash( "y", 0.0f ) );
		iTween.MoveTo( labelScore.gameObject, iTween.Hash( "y", 1.0f, "time", 1.0f ) );
		iTween.FadeFrom( labelScore.gameObject, iTween.Hash( "alpha", 1.0f ) );
		iTween.FadeTo( labelScore.gameObject, iTween.Hash( "alpha", 0.0f, "time", 1.0f, "oncomplete", "hideScore" ) );
	}
	
	public void hideScore() {
		labelScore.renderer.enabled = false;
	}
	
	/*public void clearValues( ) {
		label1.text = "";
		label2.text = "";
		label3.text = "";
	}

	public void setValues( int value1, int value2, int value3 ) {
		label1.text = string.Format ("{0}", value1);
		label2.text = string.Format ("{0}", value2);
		label3.text = string.Format ("{0}", value3);
	}*/
}
